/**
 * 测试用例导入接口测试用例
 *
 * Created by qinmengchao on 2020/06/18.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const randomString = require('randomstring');
const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmarks = [require('./../../../benchmark/generate-tests/self_designed')];

module.exports = describe('/PUT project-versions/:id/tests.json', function () {
  const administrator = new Administrator();
  let project = {};
  const newUserIds = [];
  function url(versionId) {
    return `/project-versions/${versionId}/tests.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'put');
  common.should.be.tester(uri, url, 'put');

  // 正常发起请求
  benchmarks.forEach((benchmark) => {
    let preJSON;
    let otherProjectVersion;
    let otherTester;

    it(`it should IMPORT all tests of ${benchmark.name}`, function (done) {
      let projectVersion;
      project.createRandomVersion()
        .then((version) => {
          projectVersion = version;
          return projectVersion.getAllTests(benchmark);
        })
        .then(() => projectVersion.exportTests())
        .then((res) => {
          preJSON = res;
          return administrator.createRandomTester();
        })
        .then((tester) => {
          otherTester = tester;
          newUserIds.push(tester.userId);
          otherTester = tester;
          return tester.createRandomProject();
        })
        .then((project) => project.createRandomVersion())
        .then((version) => {
          otherProjectVersion = version;
          return otherProjectVersion.getAllFunctions(benchmark);
        })
        .then(() => project.creator.login())
        .then(() => {
          chai.request(uri)
            .put(url(otherProjectVersion.versionId))
            .set('Authorization', `Bearer ${otherTester.token.accessToken}`)
            .attach('file', Buffer.from(JSON.stringify(preJSON)), randomString.generate(16))
            .end(() => {
              otherProjectVersion.exportTests()
                .then((postJSON) => {
                  common.should.be.sameExportTests(preJSON, postJSON);
                  done();
                })
                .catch(done);
            });
        })
        .catch(done);
    });

    // 导入需要测试人员为当前项目所有者
    it('it should fail to import tests when the project is not yours', function (done) {
      let projectVersion;
      administrator.createRandomTester()
        .then((tester) => {
          newUserIds.push(tester.userId);
          return tester.createRandomProject();
        })
        .then((project) => project.createRandomVersion())
        .then((version) => {
          projectVersion = version;
          return project.creator.login();
        })
        .then(() => {
          chai.request(uri)
            .put(url(projectVersion.versionId))
            .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
            .attach('file', Buffer.from('{}'), randomString.generate(16))
            .end((err, res) => {
              res.should.have.status(404);
              common.should.be.errorMessage(res.body);
              done();
            });
        })
        .catch(done);
    });

    // 不可导入不存在的项目文件的测试用例
    it('it should fail to import tests when the project is not exist', function (done) {
      let projectId;
      let projectVersion;
      project.creator.createRandomProject()
        .then((project) => {
          projectId = project.projectId;
          return project.createRandomVersion();
        })
        .then((createdVersion) => {
          projectVersion = createdVersion;
          return project.creator.removeProject(projectId);
        })
        .then(() => project.creator.login())
        .then(() => {
          chai.request(uri)
            .put(url(projectVersion.versionId))
            .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
            .attach('file', Buffer.from('{}'), randomString.generate(16))
            .end((err, res) => {
              res.should.have.status(404);
              common.should.be.errorMessage(res.body);
              done();
            });
        })
        .catch(done);
    });
  });
});
